package NiuKe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NaughtyDevil {
    public static void main(String[] args) {
        char[][] potatoes = {
//                {'Z','P','Z','P','Z'},
//                {'P','*','P','Z','P'},
//                {'Z','P','Z','P','Z'},
//                {'P','Z','P','Z','P'},
//                {'Z','P','*','P','Z'},
                {'P','Z'},
                {'P','*'},
                {'P','Z'}
        };
        System.out.println(potatoGet(potatoes));
    }

    public static int potatoGet(char[][] potatoes){
        int n = potatoes.length;
        int m = potatoes[0].length;
        HashMap<Integer, Character> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int key = i * m + j + 1;
                map.put(key,potatoes[i][j]);
            }
        }
        for (Integer key : map.keySet()) {
            if(map.get(key) == '*'){
                int[] positions = positions(key, m);
                for (int position : positions) {
                    if(map.containsKey(position)){
                        map.put(position,'Z');
                    }
                }
            }
        }
        int count = 0;
        for (Character value : map.values()) {
            if(value == 'P'){
                count++;
            }
        }

        return count;
    }

    public static int[] positions(int p,int m){
        int[] ints = new int[9];
        ints[0] = p - m - 1;
        ints[1] = p - m;
        ints[2] = p - m + 1;
        ints[3] = p - 1;
        ints[4] = p;
        ints[5] = p + 1;
        ints[6] = p + m - 1;
        ints[7] = p + m;
        ints[8] = p + m + 1;
        return ints;
    }

    public static int solution(char[][] potatoes){
        int n = potatoes.length;
        int m = potatoes[0].length;
        for (int i = 0; i < potatoes.length; i++) {
            for (int j = 0; j < potatoes[i].length; j++) {
                if(potatoes[i][j] == '*'){
                    List<Position> positions = del(i, j);
                    for (Position position : positions) {
                        if(
                                (position.x >= 0 && position.x < n)
                                && (position.y >= 0 && position.y < m)
                        ){
                            potatoes[position.x][position.y] = 'Z';
                        }
                    }
                }
            }
        }
        int count = 0;
        for (char[] potato : potatoes) {
            for (char ch : potato) {
                if (ch == 'P') {
                    count++;
                }
            }
        }
        return count;
    }
    public static List<Position> del(int x, int y){
        int n = x - 1;
        int m = y - 1;
        ArrayList<Position> positions = new ArrayList<>();
        for (int i = n; i < n + 3; i++) {
            for (int j = m; j < m + 3; j++) {
                positions.add(new Position(i,j));
            }
        }
        return positions;
    }
    static class Position{
        int x;
        int y;
        public Position(int x,int y){
            this.x = x;
            this.y = y;
        }
    }
}
